import getIn from '../../../yali-sdk/utils/getIn';
import config from '../../config';
import getLocation from '../../common/location';
import { HOME_PATH, PRODUCT_DETAIL_PATH, LOGIN_PROMPT, ORDER_SUBMIT_PATH, SHOP_LIST, MY_RESERVE_LIST } from '../pathUrl';

// 核心类
import Core from '../../../yali-sdk/core/Core';
import { login } from '../../../yali-sdk/utils/wxLogin';
import ActionDispatcher from '../../../yali-sdk/utils/ActionDispatcher';
import { addNotifier, removeNotifier, notify } from '../../../yali-sdk/utils/NotifyRegistryCenter';
const pageConfig = {
    data: {
        status: undefined,
        option: undefined,
        launch: undefined,
    },
    onUnload() {
    },

    reload(option, isHas) {
        const self = this;
        // 是否扫码进来
        const redirect_url = getIn(option, ['redirect_url']) && unescape(getIn(option, ['redirect_url']));
        const scene = getIn(option, ['scene']) && unescape(getIn(option, ['scene']));
        wx.getSystemInfo({
            success(res) {
                const version = res.SDKVersion.replace(/\./g, '');
                if (parseInt(version, 10) < 163) {
                    wx.showModal({
                        content: '当前微信版本过低,为了正常使用请先升级再使用',
                        success(res) {

                        },
                    });
                    return;
                }
                if (!global.ActionDispatcher) {
                  const Core = {
                    apiBase: 'https://www.nlido.com',
                    domain: 'mp.mini1.xinpear.net'
                  }
                  const actionDispatcher = new ActionDispatcher(Core);
                  global.ActionDispatcher = actionDispatcher;
                }
                getLocation();
                self.fetchLogin();
                if (redirect_url) {
                  // 如果是扫码进来 登录成功后跳转。
                  self.fetchLogin((callback) => {
                    wx.redirectTo({
                      url: redirect_url,
                    });
                  });
                } else if (scene) {
                  self.fetchLogin((callback) => {
                    const bind = scene.split('=').length > 1 ? scene.split('=')[1] : undefined;
                    const shop_id = bind && bind.split('&').length > 0 && bind.split('&')[0];
                    const phone = bind && bind.split('&').length > 1 && bind.split('&')[1];
                    wx.redirectTo({
                      url: `/src/pages/getcash/index?shop_id=${shop_id}&phone=${phone}`,
                    });
                  });
                } else {
                  self.toAssemble();
                }
            },
        });
    },
    // cyclelife
    onLoad(option) {
        this.data.option = option;
        this.reload(option);
    },
    fetchLogin(callback) {
    // 进来先看有没有缓存token，如果没有的情况重新授权请求，有的情况把token全局并且放到reducer；
        const token = wx.getStorageSync('token');
        if (!token) {
            login((code, res) => {
                if (code == 'SUCCESS') {
                    const token = res.data.token;
                    callback && callback();
                }
            });
        } else {
            notify('LOGIN_NOTI', token);
            getApp().globalData.token = token;
            callback && callback();
        }
    },
    // 获取当前缓存中的数据加载到reducer
    toAssemble() {
        global.navigateHelper.redirectTo({
          pathname: HOME_PATH,
          // pathname: '/src/pages/getcash/index',
        });
    }
};
Page(pageConfig);
